import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:mesh/function/widgets/border_widget.dart';
import '../../function/widgets/toast_widget.dart';
import '../../function/utils/mesh_url.dart';
import '../../function/states/user_state.dart';

/// 设置页面
class UserConfigCenter extends StatefulWidget {
  const UserConfigCenter({super.key});

  @override
  State<UserConfigCenter> createState() => _UserConfigCenterState();
}

class _UserConfigCenterState extends State<UserConfigCenter> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/common_bg.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: SafeArea(
          child: Column(
            children: [
              // 顶部导航栏
              _buildTopNavigationBar(),

              // 设置内容
              Expanded(child: _buildSettingsContent()),
            ],
          ),
        ),
      ),
    );
  }

  /// 构建顶部导航栏
  Widget _buildTopNavigationBar() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 0.h),
      child: Row(
        children: [
          GestureDetector(
            onTap: () => Get.back(),
            child: Icon(Icons.chevron_left, color: Colors.white, size: 22.sp),
          ),
          SizedBox(width: 16.w),
          Text('Settings')
              .fontSize(18.sp)
              .textColor(Colors.white)
              .fontFamily(
                GoogleFonts.quicksand(fontWeight: FontWeight.w700).fontFamily,
              )
              .fontWeight(FontWeight.w700),
        ],
      ),
    );
  }

  /// 构建设置内容
  Widget _buildSettingsContent() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 20.w),
      child: Column(
        children: [
          SizedBox(height: 40.h),

          // 设置选项列表
          _buildSettingsList(),

          const Spacer(),

          // 退出登录按钮
          _buildLogOutButton(),

          SizedBox(height: 40.h),
        ],
      ),
    );
  }

  /// 构建设置选项列表
  Widget _buildSettingsList() {
    return Column(
      children: [
        _buildSettingsItem(
          title: 'Privacy Policy',
          onTap: () => _openPrivacyPolicy(),
        ),

        SizedBox(height: 24.h),

        _buildSettingsItem(
          title: 'Terms Of Service',
          onTap: () => _openTermsOfService(),
        ),

        SizedBox(height: 24.h),

        _buildSettingsItem(
          title: 'Delete Account',
          onTap: () => _showDeleteAccountDialog(),
          isDestructive: true,
        ),
      ],
    );
  }

  /// 构建设置项
  Widget _buildSettingsItem({
    required String title,
    required VoidCallback onTap,
    bool isDestructive = false,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: double.infinity,
        child: Row(
          children: [
            Expanded(
              child: Text(title)
                  .fontSize(16.sp)
                  .textColor(
                    isDestructive ? const Color(0xFFFF6B6B) : Colors.white,
                  )
                  .fontWeight(FontWeight.w700)
                  .fontFamily(
                    GoogleFonts.quicksand(
                      fontWeight: FontWeight.w700,
                    ).fontFamily,
                  ),
            ),

            Icon(Icons.arrow_forward_ios, color: Colors.white, size: 16.w),
          ],
        ),
      ),
    );
  }

  /// 构建退出登录按钮
  Widget _buildLogOutButton() {
    return GestureDetector(
      onTap: () => _showLogOutDialog(),
      child: Container(
        width: 208.w,
        padding: EdgeInsets.symmetric(vertical: 12.h),
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
            colors: [const Color(0xFF852C12), const Color(0xFF855516)],
          ),
          borderRadius: BorderRadius.circular(15.r),
        ),
        child: Text('Log Out')
            .fontSize(16.sp)
            .textColor(Colors.white)
            .fontWeight(FontWeight.w700)
            .fontFamily(
              GoogleFonts.quicksand(fontWeight: FontWeight.w700).fontFamily,
            )
            .textAlignment(TextAlign.center),
      ).configBorderWidget(
        borderRadius: 15.r,
        borderColor: Colors.white,
        borderWidth: 0.4.w,
      ),
    );
  }

  /// 打开隐私政策
  void _openPrivacyPolicy() {
    MeshUrl.launchPrivacy();
  }

  /// 打开服务条款
  void _openTermsOfService() {
    MeshUrl.launchTerms();
  }

  /// 显示删除账号对话框
  void _showDeleteAccountDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          backgroundColor: const Color(0xFF663507),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16.r),
            side: BorderSide(color: const Color(0xFFFF8A3C), width: 1.w),
          ),
          title: Text('Delete Account')
              .fontSize(18.sp)
              .textColor(Colors.white)
              .fontWeight(FontWeight.w700)
              .textAlignment(TextAlign.center),
          content: Text(
                'Are you sure you want to delete your account? This action cannot be undone.',
              )
              .fontSize(14.sp)
              .textColor(Colors.white.withOpacity(0.8))
              .textAlignment(TextAlign.center),
          actions: [
            Row(
              children: [
                Expanded(
                  child: GestureDetector(
                    onTap: () => Navigator.of(context).pop(),
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 12.h),
                      decoration: BoxDecoration(
                        color: Colors.white.withOpacity(0.2),
                        borderRadius: BorderRadius.circular(8.r),
                      ),
                      child: Text('Cancel')
                          .fontSize(14.sp)
                          .textColor(Colors.white)
                          .fontWeight(FontWeight.w600)
                          .textAlignment(TextAlign.center),
                    ),
                  ),
                ),

                SizedBox(width: 12.w),

                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      Navigator.of(context).pop();
                      _deleteAccount();
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 12.h),
                      decoration: BoxDecoration(
                        color: const Color(0xFFFF6B6B),
                        borderRadius: BorderRadius.circular(8.r),
                      ),
                      child: Text('Delete')
                          .fontSize(14.sp)
                          .textColor(Colors.white)
                          .fontWeight(FontWeight.w600)
                          .textAlignment(TextAlign.center),
                    ),
                  ),
                ),
              ],
            ),
          ],
        );
      },
    );
  }

  /// 显示退出登录对话框
  void _showLogOutDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          backgroundColor: const Color(0xFF663507),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16.r),
            side: BorderSide(color: const Color(0xFFFF8A3C), width: 1.w),
          ),
          title: Text('Log Out')
              .fontSize(18.sp)
              .textColor(Colors.white)
              .fontWeight(FontWeight.w700)
              .textAlignment(TextAlign.center),
          content: Text('Are you sure you want to log out?')
              .fontSize(14.sp)
              .textColor(Colors.white.withOpacity(0.8))
              .textAlignment(TextAlign.center),
          actions: [
            Row(
              children: [
                Expanded(
                  child: GestureDetector(
                    onTap: () => Navigator.of(context).pop(),
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 12.h),
                      decoration: BoxDecoration(
                        color: Colors.white.withOpacity(0.2),
                        borderRadius: BorderRadius.circular(8.r),
                      ),
                      child: Text('Cancel')
                          .fontSize(14.sp)
                          .textColor(Colors.white)
                          .fontWeight(FontWeight.w600)
                          .textAlignment(TextAlign.center),
                    ),
                  ),
                ),

                SizedBox(width: 12.w),

                Expanded(
                  child: GestureDetector(
                    onTap: () {
                      Navigator.of(context).pop();
                      _logOut();
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 12.h),
                      decoration: BoxDecoration(
                        color: const Color(0xFFFF8A3C),
                        borderRadius: BorderRadius.circular(8.r),
                      ),
                      child: Text('Log Out')
                          .fontSize(14.sp)
                          .textColor(Colors.white)
                          .fontWeight(FontWeight.w600)
                          .textAlignment(TextAlign.center),
                    ),
                  ),
                ),
              ],
            ),
          ],
        );
      },
    );
  }

  /// 删除账号
  void _deleteAccount() {
    // 实际逻辑是退出登录
    UserState.performUserLogout();

    ToastWidget.showSuccess('Account deleted successfully');

    // 跳转到主页
    Get.offAllNamed('/home_base');
  }

  /// 退出登录
  void _logOut() {
    UserState.performUserLogout();

    ToastWidget.showSuccess('Logged out successfully');

    // 跳转到主页
    Get.offAllNamed('/home_base');
  }
}
